//
//  XTCProductDetailImgShow.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/3/31.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "XTCProductDetailImgShow.h"
#import "XXTool.h"
#import "DKPhotoBrowser.h"

@interface XTCProductDetailImgShow ()<DKPhotoBrowserDelegate>

@property (nonatomic,strong) UILabel* pImgLabel;

@property (nonatomic,strong) NSMutableArray* imgArray;

@property (nonatomic,strong) NSArray* imgUrlArray;

@end

@implementation XTCProductDetailImgShow
{
    CGFloat imgWidth;
}

-(UILabel*)pImgLabel
{
    if (_pImgLabel==nil)
    {
        _pImgLabel = [[UILabel alloc] init];
        _pImgLabel.textColor = [UIColor blackColor];
        _pImgLabel.text = NSLocalizedString(@"产品图片", nil);
        _pImgLabel.font = [XXTool systemFontOfSize:16.5f];
        [self addSubview:_pImgLabel];
    }
    return _pImgLabel;
}

-(NSMutableArray*)imgArray
{
    if (_imgArray==nil)
    {
        _imgArray = [[NSMutableArray alloc] init];
    }
    return _imgArray;
}

-(void)initParameter
{

}

-(void)initUI
{
    [self pImgLabel];
    [self imgArray];
}

-(void)refreshLayout:(CGRect)frame
{
    imgWidth = (frame.size.width-20*2-4*2)/3;
    
    _pImgLabel.frame = CGRectMake(20
                                  , 13
                                  , frame.size.width-40
                                  , 17);
    for (int i=0; i<_imgArray.count; i++)
    {
        UIImageView *temp = [_imgArray objectAtIndex:i];
        temp.frame = CGRectMake(_pImgLabel.frame.origin.x+(imgWidth+4)*i
                                , _pImgLabel.frame.size.height+_pImgLabel.frame.origin.y+12
                                , imgWidth
                                , imgWidth);
    }
}

-(void)showFullImage:(UIGestureRecognizer*)recognizer
{
    UIView *clickImage = [recognizer view];
    DKPhotoBrowser *dkPhotoB = [[DKPhotoBrowser alloc] init];
    dkPhotoB.delegate = self;
    [dkPhotoB setCurrentIndex:clickImage.tag];
    [dkPhotoB show];
}

#pragma mark DKPhotoModelDelegate
-(DKPhotoModel*)photoBrowser:(DKPhotoBrowser *)browser imageUrlForIndex:(NSInteger)index
{
    DKPhotoModel *useType = [DKPhotoModel new];
    useType.url = _imgUrlArray[index];
    useType.type = DKPhotoObjectTypePicture;
    useType.rectView = _imgArray[index];
    return useType;
}

-(NSInteger)numberOfPhotoBrowser:(DKPhotoBrowser *)browser
{
    return _imgArray.count;
}

-(void)setcellWithData:(NSArray *)imgUrlArray
{
    for (int i=0; i<_imgArray.count; i++)
    {
        [[_imgArray objectAtIndex:i] removeFromSuperview];
    }
    [_imgArray removeAllObjects];
    
    _imgUrlArray = imgUrlArray;
    for (int i=0; i<imgUrlArray.count; i++)
    {
        if (imgUrlArray[i]==nil||[imgUrlArray[i] isEqual:[NSNull null]])
        {
            continue;
        }
        UIImageView *temp = [[UIImageView alloc] init];
        temp.contentMode = UIViewContentModeScaleAspectFill;
        temp.clipsToBounds = YES;
        temp.userInteractionEnabled = YES;
        temp.tag = i;
        [temp setImageWithUrl:imgUrlArray[i] placerHolder:[UIImage imageNamed:@"默认展品图"]];
        [temp addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showFullImage:)]];
        [self addSubview:temp];
        [_imgArray addObject:temp];
    }
    [self refreshLayout:self.frame];
}

+(CGFloat)getCellHight
{
    CGFloat imgWidth = ([UIScreen mainScreen].bounds.size.width-20*2-4*2)/3;
    return imgWidth+13*2+17+12;
}

@end
